//
//  SISSTakeLookDetailPassengerViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/2.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSHouseCoopTakeLookDetailViewController.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSNetworkingManager+CoopProgress.h"
#import "SISSHouseCoopProgressModel.h"
#import "SISSPassengerInfoView.h"
#import "SISSHouseTakeLookDetailModel.h"
#import "SISSHouseCoopTakeLookModel.h"

@interface SISSHouseCoopTakeLookDetailViewController ()
/* 客源信息View */
@property (weak, nonatomic) SISSPassengerInfoView *passengerInfoView;
/* 带看详情模型 */
@property (strong, nonatomic) SISSHouseTakeLookDetailModel *takeLookDetailModel;

@end

@implementation SISSHouseCoopTakeLookDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

- (void)setTakeLookDetailModel:(SISSHouseTakeLookDetailModel *)takeLookDetailModel {
    _takeLookDetailModel = takeLookDetailModel;
    
    NSMutableArray *imagesURLStrings = [NSMutableArray arrayWithCapacity:takeLookDetailModel.takeLookPic.count];
    for (SISSTakeLookPicModel *model in takeLookDetailModel.takeLookPic) {
        [imagesURLStrings addObject:model.picPath];
    }
    
    self.cycleScrollView.imageURLStringsGroup = imagesURLStrings;
    self.imagesURLStrings = imagesURLStrings.copy;
    NSInteger index = takeLookDetailModel.takeLookPic.count > 0 ? 1 : 0;
    self.pageLabel.text = [NSString stringWithFormat:@"%zd/%zd",index,self.imagesURLStrings.count];
   
    takeLookDetailModel.type = self.houseCoopProgressModel.type;
    self.passengerInfoView.takeLookDetailModel = takeLookDetailModel;
}


- (void)setHouseCoopProgressModel:(SISSHouseCoopProgressModel *)houseCoopProgressModel {
    _houseCoopProgressModel = houseCoopProgressModel;
    self.type = houseCoopProgressModel.type;
    self.isCooperationIn = YES;
    SISSPassengerInfoView *passengerInfoView = [[SISSPassengerInfoView alloc] init];
    self.passengerInfoView = passengerInfoView;
    self.infoView = passengerInfoView;
}

/**
 删除带看
 */
- (void)deleteTakeLook {
    
    [kNetworkManager deleteHouseCooperationTakeLookWithTakeLookId:self.takeLookModel.ID success:^(id responseObject) {
        
        if (self.deleteCompletedBlock) {
            self.deleteCompletedBlock();
        }
        [self.navigationController popViewControllerAnimated:YES];
        
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}


/**
 带看是否属实

 @param state 1：同意 2：不同意
 */
- (void)takeLookTrueOrNotWithState:(NSInteger)state {
    
    [kNetworkManager updateHouseCooperationTakeLookWithAhcrId:self.takeLookModel.ID state:state success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) {
            
            self.brokerState = state;
            [self bottomButtonAssinment];
        }
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

/**
 加载评论数据
 */
- (void)requestCommentData {
    
    WeakSelf
    [kNetworkManager requestCommentBrokerCommentBrokerInfoWithAcbId:self.houseCoopProgressModel.ID atlrId:self.takeLookModel.ID commentType:1 coooperationType:1 success:^(NSArray<SISSCommentModel *> *commentArr) {
        
        if (commentArr.count == 0) {
            weakSelf.coopToCommentView.hidden = YES;
            weakSelf.beCommentedView.hidden = YES;
        }
        
        for (SISSCommentModel *commentModel in commentArr) {
            
            if ([commentModel.sp_id isEqualToString:[SISSUserAccount shared].spId]) {
                weakSelf.coopToCommentView.hidden = NO;
                weakSelf.coopToCommentView.commentModel = commentModel;
                
                NSString *buttonStr;
                if (commentModel.lstOfAdditional.firstObject) {
                    buttonStr = @"已评价";
                    weakSelf.bottomButton.userInteractionEnabled = NO;
                } else {
                    weakSelf.toAcrId = commentModel.ID;
                    weakSelf.isAdditionalComment = YES;
                    buttonStr = @"追加";
                }
                [self.bottomButton setTitle:buttonStr forState:UIControlStateNormal];
            } else {
                 weakSelf.beAcrId = commentModel.ID;
                weakSelf.beCommentedView.hidden = NO;
                weakSelf.beCommentedView.commentModel = commentModel;
            }
        }
        
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];

}

/**
 带看详情
 */
- (void)requestTakeLookDetailData {
    
    WeakSelf
    [kNetworkManager requestHouseCooperationTakeLookDetailWithAhcrId:self.takeLookModel.ID success:^(SISSHouseTakeLookDetailModel *takeLookDetailModel) {
        
        weakSelf.takeLookDetailModel = takeLookDetailModel;
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];

}

/**
 评论
 */
- (void)addComment {
    
    WeakSelf
//    NSString *content = [NSString unicodeToISO88591:self.midView.commentTextView.text];
    NSString *content = self.coopMidView.commentTextView.text;
    if ([NSString stringContainsEmoji:content]) {
        [self alertActionAlertWithTitle:@"提示" message:@"暂不支持表情等特殊字符评价。"];
        return;
    }
    NSDictionary *parameter = @{
                                @"be_comment_sp_id":self.houseCoopProgressModel.sp_id,
                                @"atlrId":self.takeLookModel.ID,
                                @"acbId":self.houseCoopProgressModel.ID,
                                @"house_true_score":@(self.coopMidView.validityStar),
                                @"satisfaction_score":@(self.coopMidView.satisfyStar),
                                @"profession_score":@(self.coopMidView.businessLevelStar),
                                @"content":content,
                                @"takelook_type":@(self.takeLookModel.takeLookType),
                                @"comment_type":@(1),
                                @"cooperation_type":@(1)
                                };
    
    [kNetworkManager insertBrokerCommentBrokerWithParameters:parameter success:^(id responseObject) {
        
        [SISSTools showSVWithTitle:@"评论成功"];
        [weakSelf requestCommentData];
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];

}

/**
 追加
 */
- (void)addAdditionalComment {
    NSLog(@"%@",self.additonalTextView.text);
    WeakSelf
//    NSString *content = [NSString unicodeToISO88591:self.additonalTextView.text];
    NSString *content = self.additonalTextView.text;
    if ([NSString stringContainsEmoji:content]) {
        [self alertActionAlertWithTitle:@"提示" message:@"暂不支持表情等特殊字符评价。"];
        return;
    }
    [kNetworkManager insertAdditionalCommentWithAcrId:self.toAcrId content:content commentType:7 success:^(id responseObject) {
        
        [SISSTools showSVWithTitle:@"评论成功"];
        [weakSelf requestCommentData];
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
